A TCAM (ternary content-addressable memory) is a memory for querying content based on binary numeral system, and is mainly configured to classify packets. Several entries are set in a TCAM, and an action corresponding to a TCAM entry is stored in an RAM (random-access memory). In a process of packet classification, a keyword in a packet is matched with an entry in the TCAM, and if an entry successfully matches the keyword in the packet, an action corresponding to the entry is performed on the packet.
Although using a TCAM for content query has an advantage of high efficiency, the TCAM is generally costly and power-consuming, and using a TCAM for packet classification is relatively widely applied, which leads to a shortage of TCAM resources on a network device.
In the prior art, the following three states may be used for a bit of a TCAM entry: a “0” state, a “1” state, and an “*” (Don't Care) state, where “0” and “1” may be used for exact matching, and “*” may be used for fuzzy matching. In order to save TCAM resources, a TCAM entry may be set by using “*”. For example, an action 1 needs to be performed on packets with port numbers from 0 to 7 in IP packets, and a TCAM entry 1 corresponding to the action 1 may be set to “0***”; because multiple matching conditions can be represented by using one TCAM entry, utilization of the TCAM can be improved. However, the method for setting a TCAM entry is only applicable to a situation where an entry is a suffix mask, that is, the entry only includes continuous Don't Care states and the least significant bit of the entry is a Don't Care state.